package net.e4net.watrix.example.common;

import java.io.InputStream;
import java.text.MessageFormat;
import java.util.Properties;

public class ExamProperties {
    public static final String PROPERTIES = "example.properties";
    private static ExamProperties examProperties;

    public static final synchronized ExamProperties getInstance() {
        if ( examProperties == null ) {
            examProperties = new ExamProperties();
        }
        return examProperties;
    }

    private Properties prop;

    private ExamProperties() {
        this.prop = new Properties();
        InputStream is = null;
        try {
            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            if ( classLoader == null ) classLoader = ExamProperties.class.getClassLoader();
            is = classLoader.getResourceAsStream(ExamProperties.PROPERTIES);
            this.prop.load( is );
        } catch (Throwable t) {
            t.printStackTrace();
        } finally {
            try { is.close(); } catch (Throwable ignore) {}
        }
    }

    public String getProperty(String property) {
        return this.prop.getProperty(property,"");
    }

    private static String getMessage(String key, String[] args) {
        ExamProperties examProp = ExamProperties.getInstance();
        String msg = examProp.getProperty(key);
        if ( args.length!=0 ) {
            msg = new MessageFormat(msg).format(args);
        }
        return msg;
    }

    public static String getMessage(String key) {
        return ExamProperties.getMessage(key, new String[]{});
    }

    public static String getMessage(String key, String arg1) {
        return ExamProperties.getMessage(key,new String[]{arg1});
    }

    public static String getMessage(String key, String arg1, String arg2) {
        return ExamProperties.getMessage(key,new String[]{arg1,arg2});
    }

    public static String getMessage(String key, String arg1, String arg2, String arg3) {
        return ExamProperties.getMessage(key,new String[]{arg1,arg2,arg3});
    }

    public static String getMessage(String key, String arg1, String arg2, String arg3, String arg4) {
        return ExamProperties.getMessage(key,new String[]{arg1,arg2,arg3,arg4});
    }
}